﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using EraNet.Services.Drivings;
using EraNet.Common;
using EraNet.WebApp.AppCore;
using EraNet.Services.SysManage;
using EraNet.Core.Model.SysManage;
using EraNet.Core.Model.Enum;
using EraNet.Services.Messages;

namespace EraNet.WebApp.IMS
{
    public class CommonController : IMSBasePage
    {
        #region Fields
        private readonly IDriveSchoolService _driveschoolService;
        private readonly ISysConfigService _sysConfigService;
        private readonly INoticeServices _noticeService;
        #endregion

        #region Constructors
        public CommonController(IDriveSchoolService driveschoolService, ISysConfigService sysConfigService, INoticeServices noticeService)
        {
            this._driveschoolService = driveschoolService;
            this._sysConfigService = sysConfigService;
            this._noticeService = noticeService;
        }
        #endregion

        public ActionResult _Menu()
        {
            return View();
        }

        public ActionResult _TopNav()
        {
            var m = _driveschoolService.GetLoginClient();
            return View(m);
        }

        public ActionResult _RightNav()
        {
            var m = _driveschoolService.GetLoginClient();
            var school = _driveschoolService.GetSchoolById(m.SchoolID);
            //公告
            var notic = _noticeService.SelectPager(m.SchoolID, 0, 5);

            ViewBag.schoolaccount = school;
            ViewBag.notice = notic;

            return View();
        }

        public JsonResult setPassword(string oldPwd, string newPwd, string ConfigNewPwd)
        {
            string msg = string.Empty;
            #region 为空校验
            //身份证号不能为空
            if (string.IsNullOrEmpty(oldPwd))
                return Json("1", JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(newPwd))
                return Json("2", JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(ConfigNewPwd))
                return Json("3", JsonRequestBehavior.AllowGet);
            #endregion

            var m = _driveschoolService.GetLoginClient();

            if (m.Password != Utility.MD5(oldPwd))
            {
                return Json("4", JsonRequestBehavior.AllowGet);
            }

            if (newPwd != ConfigNewPwd)
            {
                return Json("5", JsonRequestBehavior.AllowGet);
            }

            var emp = _driveschoolService.GetDrivSchoolByName(m.AccountName);
            emp.Password = Utility.MD5(newPwd);
            if (_driveschoolService.UpdateEmployee(emp) > 0)
            {
                return Json("6", JsonRequestBehavior.AllowGet);
            }



            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 注销登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOffMember()
        {
            EraNet.Common.Utility.WriteCookie("DriveSchool_Member", "");

            return RedirectToAction("Index", "Account");
        }

        public JsonResult setScore(int score)
        {
            /*
             * 1.获取登录的驾校
             * 2.判断该驾校是否有配置
             * 2.1有→修改
             * 2.2无→新增
             */
            
            //数据超出范围时设置失败
            if (score < 0 || score > 100)
            {
                return Json("0");
            }

            string result = "0";
            string cfgKeys = LMSConfigs.Exam_CfgKeys;//分数设置
            //获取登录的驾校
            var emp = _driveschoolService.GetLoginClient();
            if (emp != null)
            {
                //根据cfgkeys和schoolid获取配置
                var syc = _sysConfigService.GetEntityByFunc(sc => sc.CfgKeys == cfgKeys && sc.SchoolId == emp.SchoolID);
                if (syc == null)
                {
                    //无→新增
                    syc = new SysConfigs();
                    syc.CfgKeys = cfgKeys;
                    syc.CfgType = (int)ConfigType.IMS;
                    syc.CfgVaule = score.ToString();
                    syc.CreateDate = DateTime.Now;
                    syc.Creater = emp.AccountName;
                    syc.SchoolId = emp.SchoolID;
                    result = _sysConfigService.InsertConfig(syc).ToString();
                }
                else
                {
                    //有→修改
                    syc.CfgVaule = score.ToString();
                    syc.CreateDate = DateTime.Now;
                    syc.Creater = emp.AccountName;
                    result = _sysConfigService.Update(syc).ToString();
                }

            }

            return Json(result);
        }
    }
}
